package com.ityueqiangu.project.exhibitionInfo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ityueqiangu.core.enums.SysFlagStatusEnum;
import com.ityueqiangu.core.exception.BizException;
import com.ityueqiangu.core.util.ServletUtils;
import com.ityueqiangu.core.util.UserUtil;
import com.ityueqiangu.project.exhibitionInfo.domain.ExhibitionInfo;
import com.ityueqiangu.project.exhibitionInfo.mapper.ExhibitionInfoMapper;
import com.ityueqiangu.project.exhibitionInfo.service.IExhibitionInfoService;
import com.ityueqiangu.core.util.StringUtils;
import com.ityueqiangu.core.enums.CommonEnum;
import com.ityueqiangu.core.web.result.ResultDataUtil;
import com.ityueqiangu.project.galleryappoint.domain.GalleryAppoint;
import com.ityueqiangu.project.galleryappoint.service.IGalleryAppointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Clever、xia
 * @title: ExhibitionInfoServiceImpl
 * @description:
 * @date 2021-03-15 09:58:50
 */
@Service
public class ExhibitionInfoServiceImpl implements IExhibitionInfoService{

    @Autowired
    private ExhibitionInfoMapper exhibitionInfoMapper;
    @Autowired
    private IGalleryAppointService galleryAppointService;

    /**
     * 查询分页记录
     *
     * @return 返回集合，没有返回空List
     */
     public List<ExhibitionInfo> selectExhibitionInfoList(ExhibitionInfo exhibitionInfo) {
       return exhibitionInfoMapper.selectExhibitionInfoList(exhibitionInfo);
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回ResultInfo
     */
    public ExhibitionInfo selectExhibitionInfoById(Integer id) {
    	return exhibitionInfoMapper.selectExhibitionInfoById(id);
    }
	
    /**
     * 新增
     *
     * @param exhibitionInfo 新增的记录
     * @return 返回ResultInfo
     */
    public Integer insertExhibitionInfo(ExhibitionInfo exhibitionInfo) {
    	return exhibitionInfoMapper.insertExhibitionInfo(exhibitionInfo);
    }
	
	
    /**
     * 修改
     *
     * @param exhibitionInfo 修改的记录
     * @return 返回
     */
    public Integer updateExhibitionInfo(ExhibitionInfo exhibitionInfo) {
    	return exhibitionInfoMapper.updateExhibitionInfo(exhibitionInfo);
    }
	
  
    /**
     * 删除记录
     *
     * @param id 主键
     * @return 返回
     */
    public Integer deleteExhibitionInfoById(Integer id) {
    	return exhibitionInfoMapper.deleteExhibitionInfoById(id);
    }

    @Override
    public ExhibitionInfo existExhibitionName(ExhibitionInfo exhibitionInfo) {
        return exhibitionInfoMapper.getOne(exhibitionInfo);
    }

    @Override
    public Integer  appoint(ExhibitionInfo exhibitionInfo) {
        //查询该天是否 有展览
        List<ExhibitionInfo> exhibitionInfos = exhibitionInfoMapper.selectExhibitionListByQuery(exhibitionInfo);
        if (CollectionUtil.isEmpty(exhibitionInfos)) {
            throw new BizException("当天没有展览，请选择其它时间");
        }
        //更具id 查询 展览信息
        ExhibitionInfo exhibitionInfoResult = exhibitionInfoMapper.selectExhibitionInfoById(exhibitionInfo.getId());
        //插入预约表信息
        GalleryAppoint galleryAppoint = new GalleryAppoint();
        galleryAppoint.setGalleryId(exhibitionInfoResult.getGalleryId());
        galleryAppoint.setGalleryName(exhibitionInfoResult.getGalleryName());
        galleryAppoint.setExhibitionId(exhibitionInfoResult.getId());
        galleryAppoint.setExhibitionName(exhibitionInfoResult.getExhibitionName());
        galleryAppoint.setIsApprove(SysFlagStatusEnum.ZERO.getKey());
        galleryAppoint.setAppointUserId(UserUtil.getCurrentUser(ServletUtils.getRequest()).getId());
        galleryAppoint.setAppointUserName(UserUtil.getCurrentUser(ServletUtils.getRequest()).getLoginName());
        galleryAppoint.setAppointDate(DateUtil.parseDate(exhibitionInfo.getParams().get("appointDate").toString()));
        galleryAppoint.setTimePointKey(exhibitionInfo.getTimePointKey());
        galleryAppoint.setTimePointValue(exhibitionInfo.getTimePointValue());
//        galleryAppoint.setAppointTime(DateUtil.parse(DateUtil.format(exhibitionInfoResult.getExhibitionTime(), DatePattern.NORM_DATE_PATTERN)+" "+exhibitionInfo.getAppointTime()));
        //减少预约的数量 和增加已预约的数量
        ExhibitionInfo exhibitionInfoParam = new ExhibitionInfo();
        exhibitionInfoParam.setId(exhibitionInfoResult.getId());
        Integer appointAmount =exhibitionInfoResult.getAppointAmount()- 1;
        if (appointAmount<1||ObjectUtil.isNull(appointAmount)) {
            throw new BizException("预约数量达到最大值");
        }
        exhibitionInfoParam.setAppointAmount(appointAmount);
        Integer appointAmountFinish = exhibitionInfoResult.getAppointAmountFinish();
        if (ObjectUtil.isNull(appointAmountFinish)) {
            appointAmountFinish = 1;
        }
        exhibitionInfoParam.setAppointAmountFinish(appointAmountFinish);
        exhibitionInfoMapper.updateExhibitionInfo(exhibitionInfoParam);
        Integer appoint = galleryAppointService.insertGalleryAppoint(galleryAppoint);
        return appoint;
    }

}